package com.qk.management.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.dto.clue.UpdateClueInfoDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author diva
 * @date 2025/10/15 10:55
 * @ProjectName
 */

@Slf4j
@RestController
@RequestMapping("/businesses")
@SuppressWarnings("ALL")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    @GetMapping
    public Result list(BusinessListDTO dto) {
        log.info("查询业务统计信息");
        PageResult<BusinessDO> result = businessService.businessList(dto);
        return Result.success(result);
    }
    @PostMapping
    public Result add(@RequestBody BusinessDTO dto) {
        log.info("添加业务商机");
        businessService.addBusiness(dto);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable("id") Integer id) {
        log.info("查询业务商机");
        BusinessVO vo = businessService.getBusinessById(id);
        return Result.success(vo);
    }
    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateBusinessById(@PathVariable("businessId") Integer businessId, @PathVariable("userId") Integer userId) {
        log.info("分配业务商机，商机ID：{}，用户ID：{}", businessId, userId);
        businessService.updateById(BusinessDO.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .build()
        );
        return Result.success();
    }
    @PutMapping("/back/{id}")
    @Transactional
    public Result backBusiness(@PathVariable("id") Integer businessId) {
        log.info("退回公海池，商机ID：{}", businessId);
        businessService.update(
                Wrappers.lambdaUpdate(Business.class)
                        .set(Business::getUserId, null)
                        .set(Business::getStatus, BusinessStatusConstants.RECOVERY)
                        .set(Business::getUpdateTime, LocalDateTime.now())
                        .eq(Business::getId, businessId)
        );
        return Result.success();
    }
    @PostMapping("/toCustomer/{id}")
    @Transactional
    public Result toCustomer(@PathVariable("id") Integer businessId) {
        log.info("转换业务商机为客户商机，商机ID：{}", businessId);
        businessService.toCustomer(businessId);
        return Result.success();
    }
    @GetMapping("/pool")
    public Result pool(BusinessPoolDTO dto) {
        log.info("查询商机公海池");
        log.info("公海池查询状态: {}", dto.getStatus()); // 添加调试日志
        PageResult<Business> result = businessService.businessPool(dto);
        return Result.success(result);
    }

    @PutMapping
    public Result updateBusinessInfoById(@RequestBody UpdateBusinessInfoDTO dto){
        log.info("==============Controller中的updateBusinessInfoById处理线程名称是={}",Thread.currentThread().getName());
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }


}
